package com.gxuwz.medical.web.servlet.menu;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.MenuDao;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.tools.JsonUtil;
import com.gxuwz.medical.tools.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetMenuPidListServlet extends HttpServlet {

	DbUtil dbUtil = new DbUtil();
	MenuDao menuDao = new MenuDao();

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("进入权限树查询");
		MenuPidList(request, response);

	}

	private void MenuPidList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("PID查询");
		/*
		 * response.setContentType("text/html;charset=utf-8");
		 * request.setCharacterEncoding("utf-8"); MenuDao menuDao = new MenuDao();
		 * List<Menu> menu_tree_list = menuDao.getAllMenu(); String treeNodesJson =
		 * JSONArray.fromObject(menu_tree_list).toString();// 将list列表转换成json格式 返回
		 * 
		 * PrintWriter out = response.getWriter(); // 利用Json插件将Array转换成Json格式
		 * out.print(treeNodesJson); System.out.println("menu_tree_list=" +
		 * menu_tree_list); // 释放资源 out.close();
		 */
		Connection con = null;
		try {
			con = dbUtil.getCon();
			JSONObject jsonObject = new JSONObject();
			JSONArray jsonArray = new JSONArray();
			jsonObject.put("id", "");
			jsonObject.put("name", "请选择");
			jsonArray.add(jsonObject);

			System.out.println("JsonUtil==" + JsonUtil.formatRsToJsoarray(menuDao.MenuList(con, null, null)));
			jsonArray.addAll(JsonUtil.formatRsToJsoarray(menuDao.MenuList(con, null, null))); // 记得这里是addAll!坑爹
			ResponseUtil.write(response, jsonArray);
			System.out.println(jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception w) {
				w.printStackTrace();
			}
		}

	}
}
